package cc.higou.core.controller;

import cc.higou.core.pojo.Brand;
import cc.higou.core.pojo.SuperPojo;
import cc.higou.core.tools.Encoding;
import cc.higou.core.tools.PageHelper;
import cc.higou.service.BrandService;
import cc.higou.service.SolrService;
import org.apache.solr.client.solrj.SolrServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * 说明：
 * 门户的控制器
 *
 * @author:dongxing
 * @time:--
 */
@Controller
public class IndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private SolrService solrService;
    @Autowired
    private BrandService brandService;

    //首页的跳转
    @RequestMapping(value = "/")
    public String indexShow() {

        return "index";
    }

    //搜索条件的查询
    @RequestMapping(value = "/search")
    public String indexSearch(String keyword, Model model, String sort, Integer pageNum, Integer pageSize, Long brandId, Integer fa, Integer fb) {
        LOGGER.info("IndexController类中的indexSearch方法执行了");

        try {
            keyword = Encoding.encodeGetRequest(keyword);
            //返回原始的排序
            model.addAttribute("sort2", sort);
            //返回原始的条件查询的条件
            model.addAttribute("keyword", keyword);
            //过滤品牌的返回品牌的id
            model.addAttribute("brandId", brandId);
            //返回前台传递过来的价格的条件
            model.addAttribute("fa", fa);
            model.addAttribute("fb", fb);

            //根据条件查询相关的数据
            PageHelper.Page<SuperPojo> pojoPage = solrService.findAllFromSolr(keyword, sort, pageNum, pageSize, brandId, fa, fb);

            if ("price desc".equals(sort)) {
                sort = "price asc";
            } else {
                sort = "price desc";
            }
            //从redis中获取品牌的名称和id
            List<Brand> brandList = brandService.findAllFromRedis();
            Map<String, String> map = new TreeMap<>();
            for (Brand brand : brandList) {
                //将用户选择的品牌id和品牌名称放入集合中
                if (brand.getId().equals(brandId)) {
                    map.put("品牌", brand.getName());
                }
            }

            //将用户的价格条件添加进map集合中
            if (fa != null && fb != null) {
                if (fb == -1) {
                    map.put("价格区间", fa + "以上");
                } else {
                    map.put("价格区间", fa + "-" + fb);
                }
            }
            //将用户过滤商品的信息返回页面，以备回显
            model.addAttribute("map", map);


            model.addAttribute("brandList", brandList);

            model.addAttribute("sort", sort);
            model.addAttribute("pojoPage", pojoPage);
        } catch (SolrServerException e) {
            e.printStackTrace();
            LOGGER.error("IndexController类中的indexSearch方法的返回值有异常");
        }

        return "search";
    }
}
